package top.yj95.xiets.basic;

import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * 文章《<a href="https://xiets.blog.csdn.net/article/details/74363582" target="_balnk">JavaSwing_2.6: JTextField（文本框）</a>》中的示例代码
 * @author 杨军
 * @since 2023-12-22
 */
public class JTextFieldDemo {

    public static void main(String[] args) {
        JFrame frame = new JFrame("测试窗口");
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel();
        
        // 创建文本框，指定可见列宽数为 8 列
        JTextField textField = new JTextField(8);
        textField.setFont(new Font(null, Font.PLAIN, 20));
        panel.add(textField);
        
        // 创建一个按钮，点击后获取文本框中的文本
        JButton btn = new JButton("提交");
        btn.setFont(new Font(null, Font.PLAIN, 14));
        btn.addActionListener(e -> System.out.printf("提交：%s\n", textField.getText()));
        panel.add(btn);
        
        frame.setContentPane(panel);
        frame.setVisible(true);
    }
    
}
